package cn.tedu.boyou.comment.service.impl;

import cn.tedu.boyou.comment.domain.AnswerComment;
import cn.tedu.boyou.comment.repository.IAnswerCommentRepository;
import cn.tedu.boyou.comment.service.IAnswerCommentService;
import cn.tedu.boyou.common.query.AnswerCommentQuery;
import com.alibaba.dubbo.config.annotation.Service;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Date;
import java.util.List;

@Service
public class AnswerCommentServiceImpl implements IAnswerCommentService {
    @Autowired
    private IAnswerCommentRepository dao;
    @Autowired
    private MongoTemplate mongoTemplate;

    public AnswerComment save(AnswerComment comment) {
        comment.setCreateTime(new Date());
        AnswerComment answerComment = dao.save(comment);
        return answerComment;
    }

    public Page<AnswerComment> query(AnswerCommentQuery qo) {
        return null;
    }

    public List<AnswerComment> getCommentByTop5() {
        return null;
    }

    public List<AnswerComment> getComment(Long id) {
        Query query = new Query();
        Criteria criteria = null;
        if(id != null){
            criteria = Criteria.where("answerId").is(id);
            query.addCriteria(criteria);
            return mongoTemplate.find(query, AnswerComment.class);
        }
        return null;
    }

    public AnswerComment get(String parentId) {
        return mongoTemplate.findById(parentId,AnswerComment.class);
    }

    public void update(AnswerComment parent) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(parent.getId()));
        Update refComment = Update.update("refComment", parent.getRefComment()).set("state",1);
        UpdateResult result = mongoTemplate.updateFirst(query, refComment, AnswerComment.class);
        System.out.println(result+"-------添加后的结果");

    }

    public AnswerComment getOne(Long ans) {
        Query query = new Query();
        
        query.addCriteria(Criteria.where("answerId").is(ans));
        AnswerComment answerComment = mongoTemplate.findOne(query, AnswerComment.class);
        return answerComment;
    }

    public List<AnswerComment> getRefComment(String pid) {
        Query query = new Query();
        query.addCriteria(Criteria.where("pid").is(pid));
        List<AnswerComment> list = mongoTemplate.find(query, AnswerComment.class);
        return list;
    }

    public void updateThumbsupnum(AnswerComment comment) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(comment.getId()));
        Update thumbsupnum = Update.update("thumbsupnum", comment.getThumbsupnum());
        mongoTemplate.updateFirst(query,thumbsupnum,AnswerComment.class);
    }
}
